package com.my.biz.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.my.api.response.Msg;
import com.my.biz.dao.GroupMemberInfoMapper;
import com.my.biz.domain.GroupMemberInfoDO;
import com.my.biz.service.IGroupMemberInfoService;
import com.my.coolq.api.CoolqApi;
import com.my.coolq.api.request.AddGroupRequest;
import com.my.coolq.api.request.GroupMemberListRequest;
import com.my.coolq.api.response.BaseResponse;
import com.my.coolq.api.response.GroupMemberInfoResponse;
import com.my.core.base.dao.GenericDao;
import com.my.core.base.service.AbstractGenericService;
import com.my.util.MsgUtil;


 /**
 * 
 * @描述：IGroupMemberInfoService 服务实现类
 * 
 * @创建人： ch
 * 
 * @创建时间：2017年07月04日 21:10:39
 * 
 */
@Service("iGroupMemberInfoService")
public class GroupMemberInfoServiceImpl extends AbstractGenericService<GroupMemberInfoDO, Long> implements IGroupMemberInfoService{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupMemberInfoServiceImpl.class);
	
	@Autowired
	private CoolqApi api;
	
	@Autowired
	private GroupMemberInfoMapper groupMemberInfoMapper;
	
	@Override
    public GenericDao<GroupMemberInfoDO, Long> getDao() {
        return groupMemberInfoMapper;
    }

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addGroupMemberInfo(String uid,Long group) {
		try {
			List<GroupMemberInfoResponse> mems = api.getGroupMemberList(new GroupMemberListRequest(group));
			if(CollectionUtils.isEmpty(mems))return;
			deleteByCondition(new GroupMemberInfoDO(uid,group));
			Date time = new Date(System.currentTimeMillis());
			List<GroupMemberInfoDO> list = null;
			if(mems.size()>200){
				int size = mems.size();
				int count = size/200;
				count = mems.size()%200>0?count+1:count;
				for (int i = 0; i < count; i++) {
					list = Lists.newArrayListWithCapacity(200);
					int start = i * 200 ;
					int end = start + 200; ;
					for (int j = start; j < end && j< size; j++) {
						GroupMemberInfoResponse m = mems.get(j);
						list.add(new GroupMemberInfoDO(uid, m.getGroup_id(), m.getUser_id(), m.getNickname(), m.getSex(), m.getRole(), m.getJoin_time(), m.getCard(), m.getAge(), m.getLevel(), m.getUnfriendly().toString(), m.getTitle(), m.getTitle_expire_time(), m.getCard_changeable().toString(), m.getLast_sent_time(), m.getArea(), time));
					}
					if(CollectionUtils.isNotEmpty(list)){
						groupMemberInfoMapper.addGroupMemberInfo(list);
					}
				}
				
			}else{
				list = Lists.newArrayListWithCapacity(mems.size());
				for (GroupMemberInfoResponse m : mems) {
					list.add(new GroupMemberInfoDO(uid, m.getGroup_id(), m.getUser_id(), m.getNickname(), m.getSex(), m.getRole(), m.getJoin_time(), m.getCard(), m.getAge(), m.getLevel(), m.getUnfriendly().toString(), m.getTitle(), m.getTitle_expire_time(), m.getCard_changeable().toString(), m.getLast_sent_time(), m.getArea(), time));
				}
				groupMemberInfoMapper.addGroupMemberInfo(list);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	public Msg getMemberByUidAndGroup(String uid, String group,String start) {
		return MsgUtil.success(groupMemberInfoMapper.getMemberByUidAndGroup(uid, group, start, 40));
	}
    


}
